<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head profile="http://selenium-ide.openqa.org/profiles/test-case">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  <title>RPA IDE自动开始页</title>
</head>

<body>
  <h3>Starting Browser and RPA IDE...</h3>
  <script>
    (function () {
      var isExtensionLoaded = function () {
        const $root = document.documentElement
        return !!$root && !!$root.getAttribute('data-kantu')
      }
      var increaseCountInUrl = function (max) {
        var url = new URL(window.location.href)
        var count = 1 + (parseInt(url.searchParams.get('reload') || 0))

        url.searchParams.set('reload', count)
        var nextUrl = url.toString()

        var shouldStop = count > max
        return [shouldStop, !shouldStop ? nextUrl : null]
      }
      var run = function () {
        try {
          var evt = new CustomEvent('kantuSaveAndRunMacro', {
            detail: {
              invokeSecret: Math.random(), // 判断是否为此次调用的响应
              html: true,
              noImport: true,
              storageMode: 'browser'
            }
          })

          window.dispatchEvent(evt)
          var intervalTimer = setInterval(() => window.dispatchEvent(evt), 1000);

          if (window.location.protocol === 'file:') {
            var onInvokeSuccess = function () {
              clearTimeout(timer)
              clearTimeout(reloadTimer)
              clearInterval(intervalTimer)
              window.removeEventListener('kantuInvokeSuccess', onInvokeSuccess)
            }
            var timer = setTimeout(function () {
              alert('Error #203: It seems you need to turn on *Allow access to file URLs* for Kantu in your browser extension settings.')
            }, 8000)

            window.addEventListener('kantuInvokeSuccess', onInvokeSuccess)
          }
        } catch (e) {
          alert('Kantu Bookmarklet error: ' + e.toString());
        }
      }
      var reloadTimer = null
      var main = function () {
        if (isExtensionLoaded()) return run()

        var MAX_TRY = 3
        var INTERVAL = 1000
        var tuple = increaseCountInUrl(MAX_TRY)

        if (tuple[0]) {
          return alert('Error #204: It seems RPA IDE 未安装 - or you need to turn on *Allow access to file URLs* for RPA IDE in your browser extension settings.')
        } else {
          reloadTimer = setTimeout(function () {
            window.location.href = tuple[1]
          }, INTERVAL)
        }
      }

      setTimeout(main, 500)
    })();
  </script>
</body>

</html>

<!-- http://127.0.0.1:5500/ui.vision.html?macro=Demo%2FCore%2FDemoFrames&cmd_var1=-&cmd_var2=-&cmd_var3=-&closeRPA=1&closeBrowser=1&direct=1&savelog=logRPA_2024-12-24T07-22-23.txt&reload=3 -->